<?php

/**
 * @Entity @Table(name="property")
 */
class Model_Property {

	const CURRENCY_US = 'USD';
    const CURRENCY_ARG = 'ARS';
    
	const OPERATION_SALE = 'sale';
    const OPERATION_RENTAL = 'rental';    
    const OPERATION_V_RENTAL = 'vacation_rental';
	
    private static $operations = array(
        self::OPERATION_SALE => 'Venta',
        self::OPERATION_RENTAL => 'Alquiler',
        self::OPERATION_V_RENTAL => 'Alquiler por temporada',
    );
    
    /** @Id @Column(type="integer") @GeneratedValue */
    private $id;        

    /** @Column(type="point") */
    private $coord;
    
    /** @Column(type="text") */
    private $desc;
    
	/** @Column(type="string",length=255) */
    private $code;
    
    /** @ManyToOne(targetEntity="Model_Location", fetch="EAGER") */
    private $location;
    
    /** @ManyToOne(targetEntity="Model_Type", fetch="EAGER") */
    private $type;
    
    /** @Column(type="datetime") */
    private $publish_date;
    
    /** @Column(type="datetime") */
    private $update_date;
    
    /** @Column(type="integer") */
    private $price;
    
    /** @Column(type="string",length=255) */
    private $currency;
    
    /** @Column(type="string",length=255) */
    private $operation;
    
    /** @OneToMany(targetEntity="Model_Image", mappedBy="property", fetch="EAGER") */    
    private $images;    
    
    public function __construct() {
        $this->images = new \Doctrine\Common\Collections\ArrayCollection();
    }
    
    public static function find($id = null) {
        if (!$id) return Zend_Registry::get('em')->getRepository(get_class())->findAll();
    	return Zend_Registry::get('em')->getRepository(get_class())->find($id);
    }
    
    public static function getOperations() {
    	return self::$operations;
    }     
       
    public function filter($params) {   	
    	$qb = Zend_Registry::get('em')->createQueryBuilder();    	    	
    	
    	$qb->select('p')->from('Model_Property', 'p');
    	
    	$qb->innerJoin('p.location', 'l', 'WITH', 'l.id = :location_id');
    	$qb->setParameter('location_id', $params['location']);
    	   			
    	$qb->innerJoin('p.type', 't', 'WITH', 't.id = :type_id');
    	$qb->setParameter('type_id', $params['type']);
    	
    	$qb->where('p.operation = :operation');
    	$qb->setParameter('operation', $params['operation']);
    	
    	$currency = $params['currency'];
    	
    	$from_price = (int)$params['fromPrice'];     	
    	if ($from_price > 0) {    		
    		$qb->andWhere($qb->expr()->orx(
    		    $qb->expr()->andx($qb->expr()->gte('p.price', ':from_price'), $qb->expr()->eq('p.currency', ':currency')),
    		    $qb->expr()->andx($qb->expr()->gte('p.price', ':from_price_other_currency'), $qb->expr()->neq('p.currency', ':currency'))
    		));
    		$qb->setParameter('from_price', $from_price);
    		$qb->setParameter('from_price_other_currency', Model_Exchange::getExchangeValue($from_price, $currency));            
    	    $qb->setParameter('currency', $currency);    		
    	}
    	
    	$to_price = (int)$params['toPrice'];
    	if ($to_price > 0) {
    		$qb->andWhere($qb->expr()->orx(
    		    $qb->expr()->andx($qb->expr()->lte('p.price', ':to_price'), $qb->expr()->eq('p.currency', ':currency')),
    		    $qb->expr()->andx($qb->expr()->lte('p.price', ':to_price_other_currency'), $qb->expr()->neq('p.currency', ':currency'))
    		));    	    
            $qb->setParameter('to_price_other_currency', Model_Exchange::getExchangeValue($to_price, $currency));
    	    $qb->setParameter('to_price', $to_price);
    	    $qb->setParameter('currency', $currency);
    	}
    	
    	$query = $qb->getQuery();
    	return $query->getResult();    			
    }
    
    public function setOperation($value) {
        if (!in_array($value, self::$operations)) {
            throw new \InvalidArgumentException("Tipo de operación inválida");
        }
        $this->operation = $value;    	
    }
    
    public function setCurrency($value) {
        if (!in_array($value, array(self::CURRENCY_US, self::CURRENCY_ARG))) {
            throw new \InvalidArgumentException("Tipo de moneda inválida");
        }
        $this->currency = $value;    	
    }
    
    
    public function __get($key) {              
    	switch ($key) {
    	    case 'operation' :
    	        return self::$operations[$this->operation];
    	        break;
    	}
        return $this->{$key};  
    }
    
    public function __isset($key) { 
        return isset($this->{$key}); 
    }

}
